

package com.bei_en.teacher.admin.controller.questions;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.questions.bean.QuestionsCollection;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCollectionAddCondition;
import com.bei_en.teacher.admin.model.questions.condition.QuestionsCollectionCondition;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsCollectionVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.model.users.vo.UsersVo;
import com.bei_en.teacher.admin.other.Consts;
import com.bei_en.teacher.admin.service.questions.QuestionsCollectionService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.util.interceptor.OAuthRequired;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.bei_en.common.web.MySessionContext;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-8-21 21:28:02
 */
@Controller
@RequestMapping("/api/Questions")
public class QuestionsCollectionAPPController extends BaseController {

    @Autowired
    QuestionsCollectionService questionsCollectionService;


    @RequestMapping("/questionsCollection")
	@ResponseBody
    public JsonResult questionsCollection(Model model,@Valid  QuestionsCollectionCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<QuestionsCollectionVo> list =new ArrayList<QuestionsCollectionVo>();
		   list = questionsCollectionService.findQuestionsCollectionPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   QuestionsCollectionVo questionsCollectionVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   questionsCollectionVo=list.get(i);
	   object.put("id",  questionsCollectionVo.getId());
	   object.put("userId",  questionsCollectionVo.getUserId());
	   object.put("collectionPeople",  questionsCollectionVo.getCollectionPeople());
	   object.put("questionsId",  questionsCollectionVo.getQuestionsId());
	   object.put("createTime",  questionsCollectionVo.getCreateTime());
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/questionsCollectionAdd")
    @ResponseBody@OAuthRequired
    public JsonResult questionsCollectionAdd(Model model,@Valid  QuestionsCollectionAddCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		List<QuestionsCollectionVo> questionsCollectionVos=		questionsCollectionService.findQuestionsCollectionPage(vo);
      		if(!questionsCollectionVos.isEmpty()){
      			questionsCollectionService.delete(questionsCollectionVos.get(0).getId());
      			return new JsonSuccessResult(session.getId(), "取消收藏成功");
      		}
      		
		    QuestionsCollectionVo questionsCollection=new QuestionsCollectionVo();
		    questionsCollection.setCollectionPeople(vo.getCollectionPeople());
		    questionsCollection.setQuestionsId(vo.getQuestionsId());
		    questionsCollection.setCreateTime(new Date());
			questionsCollectionService.insert(questionsCollection);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

    @RequestMapping("/questionsCollectionEdit")
    public JsonResult questionsCollectionEdit(Model model,@Valid  QuestionsCollectionCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	QuestionsCollectionVo questionsCollection=new QuestionsCollectionVo();
	
		questionsCollectionService.updateSelective(questionsCollection);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
